<?php 
class Toos
{
	public function r($g)
	{
		if (empty($_GET[$g]))
		{
			return $_POST[$g];
		}
		else
		{
			return $_GET[$g];
		}
	}
	public function combinationGet($remove=""){
		$tmp=array();
		foreach ($_GET as $k=> $v){
			if ($k !="module" and $k!="control"){		
				if (!empty($remove)){
					if (!is_array($remove)){
						if ($remove!=$k){
							$tmp[]=$k."=".$v;
						}
					}
					else{
						if (!in_array($k,$remove)){
							$tmp[]=$k."=".$v;
						}
					}
				}
				else{
					$tmp[]=$k."=".$v;
				}
			}
		}
		
		return implode('&',$tmp);
	}
	public function msgbox($msg,$url="-1"){
		if ($url=="-1"){
			$tmp='window.history.go(-1);';
		}
		else {
			$tmp  = 'window.location.href="'.$url.'";';
		}
		exit ('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script>alert ("'.$msg.'"); '.$tmp.'</script>');
	}
	
	public function template ($path,$data=""){
		if (is_array($data) === TRUE)		{
			extract($data);
			$extract_vars = get_defined_vars();
		}
		ob_start();
		$path= WEB_DIR.DIRECTORY_SEPARATOR.'template' . DIRECTORY_SEPARATOR.$path;
		require $path;
		$text =  ob_get_contents();
		ob_clean(); 
		return  $text;
	}
	/**
	 * 下载
	 * @param unknown $file
	 */
	function download($file)
	{
 		if (file_exists($file))
 		{	
 			$file_name = basename($file);
 			$file_name = preg_replace('/\s{2, }| /','_',$file_name);
			header('Content-Description: File Transfer ');
			header('Content-Type:application/octet-stream');
			header('Content-Disposition: attachment; filename='.$file_name);
			header('Content-Transfer-Encoding: binary');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Pragma: public');
			header('Content-Length: ' . filesize($file));
			ob_clean();
			flush();
			readfile($file);
 		}
// 		if (!file_exists($file)) { //检查文件是否存在        
//			 exit;      
//		 } else {
//		 	$downloadfile = fopen($file,"r");//打开文件//
//		 	//$file=file_get_contents($file);       
//		 	header("Content-Description: File Transfer");            
//		 	Header("Content-type: application/octet-stream");         
//		 	Header("Accept-Ranges: bytes");         
//		 	Header("Accept-Length: ".filesize($file));         
//		 	Header("Content-Disposition: attachment; filename=" . basename($file));                   //
//		 	//echo $file;         
//		 	echo fread($downloadfile,filesize($file));         
//		 	fclose($downloadfile);         
//		 	exit;     
//		 }  
	}
	/**
	 * 下载
	 * @param unknown $file
	 */
	function downloadFile($file)
	{
			header('Content-Type: application/force-download');
			header('Content-Disposition: attachment; filename='.basename($file));
			header('location:'.$file);
			readfile($file);
			exit;
//		if (!file_exists($file)) { //检查文件是否存在        
//			 exit;      
//		 } else {
//		 	$downloadfile = fopen($file,"r");//打开文件//
//		 	//$file=file_get_contents($file);       
//		 	header("Content-Description: File Transfer");            
//		 	Header("Content-type: application/octet-stream");         
//		 	Header("Accept-Ranges: bytes");         
//		 	Header("Accept-Length: ".filesize($file));         
//		 	Header("Content-Disposition: attachment; filename=" . basename($file));                   //
//		 	//echo $file;         
//		 	echo fread($downloadfile,filesize($file));         
//		 	fclose($downloadfile);         
//		 	exit;     
//		 }  
	}


	function cut_str($string, $sublen ) 	{ 
		$start = 0;
		$code = 'UTF-8';
		if($code == 'UTF-8') 	{ 
			$pa ="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
			preg_match_all($pa, $string, $t_string); 
			if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; 
			return join('', array_slice($t_string[0], $start, $sublen)); 
		} 
		else 
		{ 
			$start = $start*2; 
			$sublen = $sublen*2; 
			$strlen = strlen($string); 
			$tmpstr = ''; for($i=0; $i<$strlen; $i++) 
			{ 
			if($i>=$start && $i<($start+$sublen)) 
			{ 
			if(ord(substr($string, $i, 1))>129) 
			{ 
			$tmpstr.= substr($string, $i, 2); 
			} 
			else 
			{ 
			$tmpstr.= substr($string, $i, 1); 
			} 
			} 
			if(ord(substr($string, $i, 1))>129) $i++; 
			} 
			if(strlen($tmpstr)<$strlen ) $tmpstr.= "..."; 
			return $tmpstr; 
		} 
	}

	public  function lcfirst($string){
		return  strtolower(substr($string, 0, 1)) . substr($string, 1);
	}
	/**
	 * 获取数组维数
	 */
	function getmaxdim($vDim)	{
		if(!is_array($vDim))		{
			return 0;
		}
		else		{
			$max1 = 0;
			foreach($vDim as $item1)	{
				$t1 = $this->getmaxdim($item1);
				if( $t1 > $max1){
					 $max1 = $t1;
				}
			}
			return $max1 + 1;
		}
	}
	public  function getIP(){
		if(!empty($_SERVER["HTTP_CLIENT_IP"])){
			$cip = $_SERVER["HTTP_CLIENT_IP"];
		}
		elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
			$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		elseif(!empty($_SERVER["REMOTE_ADDR"])){
			$cip = $_SERVER["REMOTE_ADDR"];
		}
		else{
			$cip = "无法获取！";
		}
		return $cip;
	}
	public   function new_sn()	{
		return date('Ymd') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
	}
	public   function request_by_curl($remote_server, $post_string)   {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $remote_server);
		if (is_array($post_string)){
			$tmp  = array();
			foreach ($post_string as $k => $v){
				$tmp[] = $k.'='.$v;
			}
			$post_string = implode('&',$tmp);
		}
		curl_setopt($ch, CURLOPT_POSTFIELDS,  $post_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	
}

?>